//
//  HJImageMarkView.m
//  KaiChat
//
//  Created by lx on 2023/11/21.
//

#import "HJProgressMarkView.h"



@interface HJProgressMarkView ()


@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, strong) CABasicAnimation *rotationAnimation;
@property (nonatomic, weak) UILabel *textLabel;

@end


@implementation HJProgressMarkView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];

        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"sendLoading"];
        _imageView = imageView;
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.height.offset(44);
        }];
        
        UILabel *textLabel = [UILabel lableWithSuperView:self font:UIFontMake(12) color:UIColor.whiteColor title:nil textAlignment:NSTextAlignmentLeft];
        _textLabel = textLabel;
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
        }];
    }
    return self;
}

- (void)startAnimating {
    
    self.hidden = NO;
 
    if ([self.imageView.layer.animationKeys containsObject:@"rotation"]) {
        return;
    }
    
    [self.imageView.layer addAnimation:self.rotationAnimation forKey:@"rotation"];
}

- (CABasicAnimation *)rotationAnimation {
    if (!_rotationAnimation) {
        _rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        _rotationAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
        _rotationAnimation.duration = 0.6;
        _rotationAnimation.repeatCount = CGFLOAT_MAX;
        _rotationAnimation.removedOnCompletion = NO;
    }
    return _rotationAnimation;
}

- (void)stopAnimating {
    [_imageView.layer removeAllAnimations];
    if (self.hidden == NO) {
        self.hidden = YES;
    }
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    if (progress <= 0) {
        _textLabel.text = nil;
    } else {
        _textLabel.text = [NSString stringWithFormat:@"%.f%%", progress * 100];
    }
}

@end
